package com.heixiu.gallery2

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.photos_preview_item.view.*

class PhotosPreviewAdapter : ListAdapter<PhotoItem, PhotosViewHolder>(DIFFCALLBACK) {

    object DIFFCALLBACK:DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.id == newItem.id
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotosViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.photos_preview_item, parent, false)
        itemView.setOnClickListener {
            Log.d("mytag", "ok")
        }
        val holder = PhotosViewHolder(itemView)
        return holder
    }

    override fun onBindViewHolder(holder: PhotosViewHolder, position: Int) {
        Glide.with(holder.itemView)
            .load(getItem(position).largePhoto)
            .placeholder(R.drawable.ic_photo_black_24dp)
            .into(holder.itemView.imageViewPhotosPreview)
    }

}

class PhotosViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)